package g4p.newg4p;

import g4p_controls.G4P;
import g4p_controls.GButton;
import g4p_controls.GCheckbox;
import g4p_controls.GEditableTextControl;
import g4p_controls.GEvent;
import g4p_controls.GKnob;
import g4p_controls.GLabel;
import g4p_controls.GTextField;
import g4p_controls.GToggleControl;
import g4p_controls.GValueControl;

import java.awt.event.KeyEvent;

import processing.core.PApplet;
import processing.core.PGraphicsJava2D;
import processing.core.PImage;

public class TextField_Test extends PApplet {

	GTextField txfSample;
	GLabel lblSample;
	GButton btnSample;

	GButton[] btnColours = new GButton[8];

	
	GKnob knbAngle;
	GCheckbox cbxJustify, cbxEnabled;

	PImage img;
	PGraphicsJava2D snapshot = null;

	String s;

	public void setup() {
		size(700,360);
		cursor(CROSS);
		img = loadImage("g4p/instr.jpg");
		int xpos = width - img.width - 4;
		int w = img.width / 8;
		
		for(int i = 0; i < btnColours.length; i++){
			String s = "" + (i+1);
			btnColours[i] = new GButton(this, xpos + i * w, img.height + 8, w, 18, s);
			btnColours[i].setLocalColorScheme(i);
			btnColours[i].tagNo = 1000 + i;
		}

		cbxJustify = new GCheckbox(this, xpos, img.height + 30, 120, 20, "Justify text");
		cbxEnabled = new GCheckbox(this, xpos, img.height + 50, 120, 20, "Enable / disable");
		cbxEnabled.setSelected(true);

		knbAngle = new GKnob(this, width - 210, height -  90, 70, 70, 10);
		knbAngle.setLimits(0, 0, TWO_PI);

		//                                x   y   w   h
		txfSample = new GTextField(this, 60, 160, 120, 26, G4P.SCROLLBARS_HORIZONTAL_ONLY); // | G4P.SCROLLBARS_AUTOHIDE);
		//                 V use float to avoid existing version with int		
//		txfSample.setText("Peter Lager loves buscuits and cheese");
		//		txaSample.setText("0123456789");
		//		txaSample.setText(" ");
		txfSample.setOpaque(true);
		txfSample.setPromptText("Please enter name");

		btnSample = new GButton(this, 10, 20, 50, 40, "Click on me");
//		btnSample.setTextNew("Click Me");
		btnSample.fireAllEvents(true);

		lblSample = new GLabel(this,40,200,80,100, "");
		lblSample.setOpaque(true);
		lblSample.setText("Peter lives in a little town in North Wales");
	}

	public void draw() {
		background(200);
		image(img, width - img.width - 4, 4);
		fill(0);
		//c0.draw();
		text("["+mouseX+","+mouseY+"]", 20,10);
		if(snapshot != null){
			image(snapshot, 0, 0);
		}
	}

	public void keyReleased(KeyEvent e){
		if(key == ' '){
			//			snapshot = (PGraphicsJava2D) txfSample.getSnapshot();
		}
	}

	public void handleTextEvents(GEditableTextControl textcontrol, GEvent event) {
		
	}

	public void handleKnobEvents(GValueControl knob, GEvent event) {
		txfSample.setRotation(knbAngle.getValueF());
		btnSample.setRotation(knbAngle.getValueF());
		lblSample.setRotation(knbAngle.getValueF());
	}

	public void handleToggleControlEvents(GToggleControl checkbox, GEvent event) { 
		if(checkbox == cbxJustify)
			txfSample.setJustify(checkbox.isSelected());
		if(checkbox == cbxEnabled)
			txfSample.setEnabled(cbxEnabled.isSelected());
	}


	public void handleButtonEvents(GButton button, GEvent event) { 
		if(button.tagNo >= 1000){
			txfSample.setLocalColorScheme(button.tagNo - 1000);
			btnSample.setLocalColorScheme(button.tagNo - 1000);
			lblSample.setLocalColorScheme(button.tagNo - 1000);
		}
		else {
			switch(event){
			case PRESSED:
				System.out.println("Button PRESEED");
				break;
			case RELEASED:
				System.out.println("Button RELEASED");
				break;
			case CLICKED:
				System.out.println("Button CLICKED");
				break;
			default:
				System.out.println("Unknown button event");
			}
		}
	}
}
